import { assetManager, Label, resources, Sprite, SpriteFrame } from "cc";

export class xstory {
  playType: boolean = false;
  background: Sprite;
  left: Sprite;
  right: Sprite;
  text: Label;
  data: {
    id: number;
    name: string;
    scene_list: {
      id: number;
      bgImg: string;
      bgImgPath: string;
      scriptList: {
        id: number;
        right: string;
        rightPath: string;
        left: string;
        leftPath: string;
        text: string;
      }[];
    }[];
  };

  sceneIndex: number = 0;
  scriptindex: number = 0;

  constructor(option: {
    background: Sprite;
    left: Sprite;
    right: Sprite;
    text: Label;
    data: {
      id: number;
      name: string;
      scene_list: {
        id: number;
        bgImg: string;
        bgImgPath: string;
        scriptList: {
          id: number;
          right: string;
          rightPath: string;
          left: string;
          leftPath: string;
          text: string;
        }[];
      }[];
    };
  }) {
    this.background = option.background;
    this.left = option.left;
    this.right = option.right;
    this.text = option.text;
    this.data = option.data;
    console.log("======>");
    console.log(this.data);
  }

  /**
   * 开始播放
   * @param params
   */
  play(start?: { sceneIndex: number; script: number }, callback?: Function) {
    if (this.playType == false) {
      if (start) {
        this.sceneIndex = start.sceneIndex;
        this.scriptindex = start.sceneIndex;
      }
      this.playType = true;
    }
    console.log("===开始播放===");
    let bgUuid = this.data.scene_list[this.sceneIndex].bgImg;
    console.log("背景资源：" + bgUuid);
    if (bgUuid) {
      assetManager.loadAny({ uuid: bgUuid }, { priority: 2, maxRetryCount: 10 }, (err, data) => {
        if (err) {
          console.log(err);
          this.background.getComponent(Sprite).spriteFrame = null;
          return;
        }
        this.background.getComponent(Sprite).spriteFrame = data;
      });
    } else {
      this.background.getComponent(Sprite).spriteFrame = null;
    }

    if (this.data.scene_list[this.sceneIndex].scriptList.length > 0) {
      let rightUuid = this.data.scene_list[this.sceneIndex].scriptList[this.scriptindex].right;
      console.log("right资源：" + rightUuid);
      if (rightUuid) {
        assetManager.loadAny({ uuid: rightUuid }, { priority: 2, maxRetryCount: 10 }, (err, data) => {
          if (err) {
            console.log(err);
            this.right.getComponent(Sprite).spriteFrame = null;
            return;
          }
          this.right.getComponent(Sprite).spriteFrame = data;
        });
      } else {
        this.right.getComponent(Sprite).spriteFrame = null;
      }

      let leftUuid = this.data.scene_list[this.sceneIndex].scriptList[this.scriptindex].left;
      console.log("left资源：" + leftUuid);
      if (leftUuid) {
        assetManager.loadAny({ uuid: leftUuid }, { priority: 2, maxRetryCount: 10 }, (err, data) => {
          if (err) {
            console.log(err);
            this.left.getComponent(Sprite).spriteFrame = null;
            return;
          }
          this.left.getComponent(Sprite).spriteFrame = data;
        });
      } else {
        this.left.getComponent(Sprite).spriteFrame = null;
      }

      let text = this.data.scene_list[this.sceneIndex].scriptList[this.scriptindex].text;
      this.text.getComponent(Label).string = text;
    }

    let storyinfo = this.checkNext(this.sceneIndex, this.scriptindex);
    if (storyinfo.next.sceneIndex == -1) {
      this.playType = false;
      this.sceneIndex = 0;
      this.scriptindex = 0;
    } else {
      this.sceneIndex = storyinfo.next.sceneIndex;
      this.scriptindex = storyinfo.next.scriptindex;
    }
    if (callback) {
      callback(storyinfo);
    }
  }

  checkNext(sceneIndex: number, scriptindex: number) {
    let storyinfo = {
      now: {
        sceneIndex: sceneIndex,
        scriptindex: scriptindex,
      },
      next: {
        sceneIndex: sceneIndex,
        scriptindex: scriptindex + 1,
      },
    };
    if (this.data.scene_list[sceneIndex].scriptList.length <= storyinfo.next.scriptindex) {
      storyinfo.next.sceneIndex = storyinfo.next.sceneIndex + 1;
      storyinfo.next.scriptindex = 0;
      if (this.data.scene_list.length == storyinfo.next.sceneIndex) {
        storyinfo.next.sceneIndex = -1;
        storyinfo.next.scriptindex = -1;
      }
    }
    return storyinfo;
  }
}
